package cn.hyxy.hadoop.hdfs;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class Demo02_HA extends Configured implements Tool{
	@Override
	public int run(String[] args) throws Exception {
		Configuration config=getConf();
		FileSystem fs = FileSystem.get(config);
		RemoteIterator<LocatedFileStatus> list = fs.listFiles(new Path("/"), true);
		while (list.hasNext()) {
			LocatedFileStatus file = list.next();
			System.out.println(file.getPath());
		}
		
		InputStream inputStream = new FileInputStream("D:/a/1.txt");
		OutputStream outputStream = fs.create(new Path("/b.txt"));
//		OutputStream outputStream = new FileOutputStream("/c.txt");//错误！！！！！！！！！！！
		IOUtils.copyBytes(inputStream, outputStream, 4096);//文件合并及上传至hdfs上
		
		outputStream.close();
		inputStream.close();
		fs.close();		
		return 0;
	}

	public static void main(String[] args) throws Exception {
		int code = ToolRunner.run(new Demo02_HA(), args);
		System.exit(code);
	}
}
